// components/findlost/findlost.js
const app = getApp()
const db = wx.cloud.database()
var sev = require('../../utils/sev.js');
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    fileList: [],
    cloudlist: [],
    btnhide: false,
    content: '',
    title: '',
    fileid:0,
    filecontent: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getTitle(e) {
      this.setData({
        title: e.detail.value
      });
    },
    textareaAInput(e) {
      this.setData({
        content: e.detail.value
      }); //赋值
    },
    fileInput(e) {
      this.setData({
        filecontent: e.detail.value
      }); //赋值
    },
    updata() {
      wx.showLoading({
        title: '上传中~',
        mask: true
      })
      //上传图片
      var that = this;
      that.setData({
        btnhide: true
      })
      var timecode = sev.vcode(new Date());
      var files = this.data.fileList;
      var len = files.length;
      console.log(len)
      var i = 0;
      for (i = 0; i < len; i++) {
        var str = files[i].name;
        wx.cloud.uploadFile({
          cloudPath: 'file/' + '(' + sev.getformatTime(new Date()) + ')' + str,
          filePath: files[i].path,
          success(res) {
            console.log(res)
            that.setData({
              cloudlist: that.data.cloudlist.concat([res.fileID]),

            })
          },
          fail(res) {
            console.log(res)
          }
        })
      }
      var timer = setInterval(function () {
        // console.log(that.data.cloudlist.length)
        if (that.data.cloudlist.length == len) {
          clearInterval(timer);
          that.toUpdate();
          // wx.hideLoading();

        }
      }, 1000)
    },
    toUpdate() {
      var that = this;
      wx.cloud.callFunction({
        name: 'kindb',
        data: {
          tablename: 'bbs',
          opr: 'add',
          data: {
            title: that.data.title,
            content: that.data.content,
            dbdate: sev.formatTime(new Date()),
            cloudlist: that.data.cloudlist,
            realList: ['https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573887937984&di=6289bc14836a61e9d8fa0a8c223b2c17&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20181111%2F2647be659c06424cb26d344f5845a96e.jpeg'],
            type: '考试资料',
            pubstuid: app.eduOS.xh,
            lostinfo: null,
            calls: '',
            mapdata: null,
            filelist: that.data.fileList,
            teacherinfo: null
          }
        },
        success(res) {
          that.setData({
            btnhide: false
          })
          wx.hideLoading();
          wx.navigateBack({

          })
        }
      })
    },
    chooseFiles() {
      var that = this;
      wx.chooseMessageFile({
        success(res) {
          var str = res.tempFiles[0].path;

          //console.log(str.substring(str.lastIndexOf('.')+1))
          console.log(res)
          that.setData({
            fileList: that.data.fileList.concat(res.tempFiles)
          })
        }
      })
    }
  }
})